--[[----------------------------------------------------------------------------
LR2SignatureSection.lua
--------------------------------------------------------------------------------
Copyright 2010 Andriy Doroshchuk
 All Rights Reserved.
------------------------------------------------------------------------------]]

require 'LR2Util_Logger'

-- Lightroom SDK
local LrView 		= import 'LrView'
local LrPathUtils 	= import 'LrPathUtils'
local LrDialogs		= import 'LrDialogs'
local LrTasks 		= import 'LrTasks'

--============================================================================--
local LR2SignatureSection = {}
-------------------------------------------------------------------------------
LR2SignatureSection.exportPresetFields = {
	{ key = 'pathToSignature',		default = ''},
	{ key = 'signatureLocation',	default = 'southeast'},
	{ key = 'offsetX',				default = 0},
	{ key = 'offsetY',				default = 0},
}
-------------------------------------------------------------------------------
function LR2SignatureSection.CreateOffsetRow(desc)
	local vf = desc.vf
	local row = {
		spacing = vf:control_spacing(),
		vf:static_text {
			title 		= desc.title,
			alignment 	= 'right',
			width 		= LrView.share 'label_width',
		},
		vf:edit_field {
			width_in_digits = 3,
			min 		= 0,
			max 		= 100,
			precision 	= 0,
			value 		= LrView.bind('offset' .. desc.tag),
		},
		vf:slider {
			min 		= 0,
			max 		= 100,
			integral 	= true,
			width 		= 200,
			value 		= LrView.bind('offset' .. desc.tag),
		},
	}
	return row
end
-------------------------------------------------------------------------------
function LR2SignatureSection.sectionForFilterInDialog( viewFactory, propertyTable )
	local vf = viewFactory
	local pt = propertyTable
	
	local ef = vf:edit_field {
		width 		= 250,
		value 		= LrView.bind 'pathToSignature',
	} 

	return {
		title 		= LOC '$$$/LR2/PhotoFrames/Signature/SectionTitle=LR2/PhotoFrames Signature', 
		synopsis	= LrView.bind {key = 'pathToSignature', object = propertyTable },
		
		vf:row{
			spacing = vf:control_spacing(),
			vf:static_text {
				title 		= LOC '$$$/LR2/PhotoFrames/Signature/PathToSignature=Path to signature:',
				alignment 	= 'right',
				width 		= LrView.share 'label_width',
			},
			ef,
			vf:push_button {
				title 		= LOC '$$$/LR2/PhotoFrames/Config/Select=Select',
				action		= function (button)
					local path = LrDialogs.runOpenPanel {
						title 	= LOC '$$$/LR2/PhotoFrames/Signature/PathToSignature=Path to signature:',
						prompt 	= LOC '$$$/LR2/PhotoFrames/Config/Select=Select',
						canChooseFiles 			= true,
						canChooseDirectories	= false,
						canCreateDirectories	= false,
						allowsMultipleSelection = false,
					}
					if path ~= nil then 
						ef.value = path[1]
					end
				end,
			},
		},
		vf:separator {
			fill_horizontal = 1,
		},
		vf:row{
			vf:group_box {
				title		= LOC '$$$/LR2/PhotoFrames/Signature/Location=Signature location',
				spacing 	= vf:control_spacing(),
				vf:row{
					vf:column{
						vf:row(LR2SignatureSection.CreateOffsetRow({vf=vf, title=LOC '$$$/LR2/PhotoFrames/Signature/OffsetX=Offset X', tag='X'})),
						vf:row(LR2SignatureSection.CreateOffsetRow({vf=vf, title=LOC '$$$/LR2/PhotoFrames/Signature/OffsetY=Offset Y', tag='Y'})),
					},
					vf:column{
						vf:row{
							vf:radio_button {
								value = LrView.bind 'signatureLocation',
								checked_value = 'northwest',
							},
							vf:radio_button {
								value = LrView.bind 'signatureLocation',
								checked_value = 'north',
							},
							vf:radio_button {
								value = LrView.bind 'signatureLocation',
								checked_value = 'northeast',
							},
						},
					
						vf:row{
							vf:radio_button {
								value = LrView.bind 'signatureLocation',
								checked_value = 'west',
							},
							vf:radio_button {
								value = LrView.bind 'signatureLocation',
								checked_value = 'center',
							},
							vf:radio_button {
								value = LrView.bind 'signatureLocation',
								checked_value = 'east',
							},
						},
						
						vf:row{
							vf:radio_button {
								value = LrView.bind 'signatureLocation',
								checked_value = 'southwest',
							},
							vf:radio_button {
								value = LrView.bind 'signatureLocation',
								checked_value = 'south',
							},
							vf:radio_button {
								value = LrView.bind 'signatureLocation',
								checked_value = 'southeast',
							},
						},
					},
				},
			},
		},
	}
	
end
-------------------------------------------------------------------------------
function LR2SignatureSection.postProcessRenderedPhotos( functionContext, filterContext )
	local renditionOptions = {
		plugin 				= _PLUGIN,
		renditionsToSatisfy = filterContext.renditionsToSatisfy,
		filterSettings 		= function( renditionToSatisfy, exportSettings )
		end,
	}

	local path2IM 	= filterContext.propertyTable.pathToIM
	local path2Sig 	= filterContext.propertyTable.pathToSignature
	local location	= filterContext.propertyTable.signatureLocation
	local offsetX	= filterContext.propertyTable.offsetX
	local offsetY	= filterContext.propertyTable.offsetY
	local command	= '-gravity ' .. location .. ' -geometry +' .. offsetX .. '+' .. offsetY .. ' "' .. path2Sig .. '"'
	
	local doPostProcess = false
	if path2IM and string.len(path2IM) then 
		doPostProcess = true
	end
	
	for sourceRendition, renditionToSatisfy in filterContext:renditions(renditionOptions) do
		local success, pathOrMessage = sourceRendition:waitForRender()
		if success and WIN_ENV == true and doPostProcess then
			local path = sourceRendition.destinationPath
			command = '""' .. LrPathUtils.child(path2IM, 'composite.exe') .. '" ' .. command .. ' "' .. path .. '" "' .. path .. '""'
			--LOG (command)
			if LrTasks.execute(command) ~= 0 then
				renditionToSatisfy:renditionIsDone( false, "Failed to render frame")
			end
		end
	end
	
end
--------------------------------------------------------------------------------

return LR2SignatureSection

